package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.Statistics_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


public class StatisticsController extends Controller {

    private final FormFactory formFactory;
    private final Statistics_Repository statistics_Repository;
    private final HttpExecutionContext ec;

    @Inject
    public StatisticsController(FormFactory formFactory, Statistics_Repository statistics_Repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.statistics_Repository = statistics_Repository;
        this.ec = ec;
    }

    public CompletionStage<Result> listByProduct() {
        JsonNode json = request().body().asJson();
        String startTime = json.findPath("startTime").textValue();
        String endTime = json.findPath("endTime").textValue();

        return statistics_Repository.listByProduct(startTime,endTime).thenApplyAsync(ResultList -> {
            return ok(Json.toJson(ResultList));
        }, ec.current());
    }

    public CompletionStage<Result> listByProductCompany() {
        JsonNode json = request().body().asJson();
        String startTime = json.findPath("startTime").textValue();
        String endTime = json.findPath("endTime").textValue();
        String productName = json.findPath("productName").textValue();

        return statistics_Repository.listByProductCompany(startTime,endTime,productName).thenApplyAsync(ResultList -> {
            return ok(Json.toJson(ResultList));
        }, ec.current());
    }

    public CompletionStage<Result> BDlistByCompany() {
        JsonNode json = request().body().asJson();
        String startTime = json.findPath("startTime").textValue();
        String endTime = json.findPath("endTime").textValue();
        String productName = json.findPath("productName").textValue();
        String companyName = json.findPath("companyName").textValue();

        return statistics_Repository.BDlistByCompany(startTime,endTime,productName,companyName).thenApplyAsync(ResultList -> {
            return ok(Json.toJson(ResultList));
        }, ec.current());
    }
}
